﻿using KendoCRUDService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KendoCRUDService.Controllers
{
    public class EmployeesOrgChartController : Controller
    {
        public ActionResult Index()
        {
            var result = from node in AllNodes
                select new
                {
                    Id = node.Id,
                    Position = node.Position,
                    FullName = node.FullName,
                    Avatar = node.Avatar,
                    ParentId = node.ParentId,
                };

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult LoadOnDemand(int? Id)
        {
            IEnumerable<EmployeeNodeModel> result;

            result = AllNodes.Where(e => e.ParentId == Id)
                .Select(e => new EmployeeNodeModel
                {
                    Id = e.Id,
                    Position = e.Position,
                    FullName = e.FullName,
                    Avatar = e.Avatar,
                    ParentId = e.ParentId,
                    Expanded = e.Expanded,
                    HasChildren = AllNodes.Where(s => s.ParentId == e.Id).Count() > 0
                });

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Create(EmployeeNodeModel model)
        {
            int lastID = AllNodes.Select(m => m.Id).Max();
            if (model.Id == 0)
            {
                model.Id = lastID + 1;
            }
            AllNodes.Add(model);

            return Json(model);
        }

        public JsonResult Destroy(EmployeeNodeModel model)
        {
            var target = One(m => m.Id == model.Id);
            AllNodes.Remove(target);

            return Json(target);
        }

        public JsonResult Update(EmployeeNodeModel model)
        {
            var target = One(m => m.Id == model.Id);

            target.Position = model.Position;
            target.FullName = model.FullName;
            target.ParentId = model.ParentId;
            target.Avatar = model.Avatar;

            return Json(target);
        }


        public static EmployeeNodeModel One(Func<EmployeeNodeModel, bool> predicate)
        {
            return AllNodes.FirstOrDefault(predicate);
        }

        private static IList<EmployeeNodeModel> AllNodes
        {
            get
            {
                IList<EmployeeNodeModel> result = System.Web.HttpContext.Current.Session["OrgChartEmployees"] as IList<EmployeeNodeModel>;

                if (result == null)
                {
                    System.Web.HttpContext.Current.Session["OrgChartEmployees"] = result = new List<EmployeeNodeModel>
                    {
                        new EmployeeNodeModel { Id = 1, Expanded = true, Position = "CEO", ParentId = null, FullName = "Michelle Lowrence", Avatar = "" },
                        new EmployeeNodeModel { Id = 2, Expanded = false, Position = "Sales and Marketing Manager", ParentId = 1, FullName = "Laura Richardson", Avatar = ""  },
                        new EmployeeNodeModel { Id = 3, Expanded = false, Position = "Financial Manager", ParentId = 1, FullName = "Robert Tomson", Avatar = "" },
                        new EmployeeNodeModel { Id = 4, Expanded = false, Position = "Sales Team Lead", ParentId = 2, FullName = "Martina Howell", Avatar = "" },
                        new EmployeeNodeModel { Id = 5, Expanded = false, Position = "Marketing Specialist", ParentId = 2, FullName = "Steven North", Avatar = "" },
                        new EmployeeNodeModel { Id = 6, Expanded = false, Position = "Accountant", ParentId = 3, FullName = "Katrin Donalds", Avatar = "" },
                        new EmployeeNodeModel { Id = 7, Expanded = false, Position = "Accountant", ParentId = 3, FullName = "Sean Rusell", Avatar = "" },
                        new EmployeeNodeModel { Id = 8, Expanded = false, Position = "Sales Representative", ParentId = 4, FullName = "Nicola Miller", Avatar = "" },
                        new EmployeeNodeModel { Id = 9, Expanded = false, Position = "Sales Representative", ParentId = 4, FullName = "Daniel Morisson", Avatar = "" },
                        new EmployeeNodeModel { Id = 10, Expanded = false, Position = "Junior Sales Representative", ParentId = 4, FullName = "Pamela Baker", Avatar = "" },
                        new EmployeeNodeModel { Id = 11, Expanded = false, Position = "Junior Accountant", ParentId = 7, FullName = "Andrew Berry", Avatar = "" }
                    };
                }
                return result;
            }
        }
    }

}